import {sendSuccess, sendError} from "../../utils/responseHandler";
import {User} from "../../../db/models/user";
import {Work} from "../../../db/models/work";
//应聘
export default async function handler(req, res) {
    if (req.method === 'PUT') {
        try {
            const {workId, userId, status} = req.body;

            //查一下是否有这个用户
            const userInfo = await User.findByPk(userId);
            //查一下该该岗位的status是否为1,1是审核通过且没招满的则允许，其他状态不允许
            const workInfo = await Work.findByPk(workId);

            if (!userInfo) {
                sendError(res, '用户不存在!', 401)
                return;
            }

            if (!workInfo) {
                sendError(res, '该岗位不存在！', 401)
                return;
            }

            //审核通过
            if (workInfo.work_status === 0) {
                await workInfo.update({
                    work_status: status === 1 ? 1 : 2,
                }, {where: {id: workId}})
                sendSuccess(res, '1');
            } else if (workInfo.status === 1) {
                sendError(res, '该岗位已通过审核,无需再次审核!')
            } else if (workInfo.status === 2) {
                sendError(res, '该岗位并未通过审核!')
            } else if (workInfo.status === 3) {
                sendError(res, '该岗位已招满人!')
            }

        } catch (error) {
            console.error('创建失败:', error);
            sendError(res, '创建失败', 500);
        }
    } else {
        res.setHeader('Allow', ['GET', 'POST']);
        res.status(405).end(`Method ${req.method} Not Allowed`);
    }
}
